package com.example.administrator.managesystem.activity.my_profile;


import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.administrator.managesystem.R;
import com.example.administrator.managesystem.activity.login.LoginActivity;
import com.example.administrator.managesystem.activity.my_profile.typein.TypeInCodeActivity;
import com.example.administrator.managesystem.base.BaseActivity;
import com.example.administrator.managesystem.ui.ProfileView;
import com.example.administrator.managesystem.utils.AppDownloadManager;
import com.example.administrator.managesystem.utils.GlobalConfig;
import com.example.administrator.managesystem.utils.ShareUtil;
import com.example.administrator.managesystem.view.CircleImageView;
import com.example.administrator.managesystem.view.LogoutDialog;
import com.hedgehog.ratingbar.RatingBar;
import com.squareup.picasso.Picasso;

import java.io.File;
import java.util.List;

/**
 * Created by Administrator on 2018/12/25.
 */

public class ProfileCenterActivity extends BaseActivity implements ProfileView {
    private LinearLayout logout_ll,my_goods,my_message,my_circle,my_share,my_collect,password_setting,check_version;
    private ImageView profile_edit;
    private RatingBar mRatingBar;
    private TextView account;
    private CircleImageView user_image;
    private AppDownloadManager appDownloadManager;

    @Override
    public void initView() {
        showfloatButton(true,false);
        appDownloadManager=new AppDownloadManager(this);
        logout_ll=findViewById(R.id.log_out_ll);
        account=findViewById(R.id.account);
        my_circle=findViewById(R.id.my_circle_ll);
        my_collect=findViewById(R.id.my_collect_ll);
        user_image=findViewById(R.id.user_image);
        my_message=findViewById(R.id.my_message_ll);
        my_share=findViewById(R.id.my_share_ll);
        my_goods=findViewById(R.id.my_goods_ll);
        password_setting=findViewById(R.id.psd_ll);
        profile_edit=findViewById(R.id.profile_edit);
        check_version=findViewById(R.id.check_version_ll);
        mRatingBar=findViewById(R.id.rate_bar);
        mRatingBar.setClickable(false);
        //设置星星的宽度
        mRatingBar.setStarImageWidth(18f);
        //设置星星的高度
        mRatingBar.setStarImageHeight(18f);
        //设置星星之间的距离
        mRatingBar.setImagePadding(5f);
        //设置填充的星星
        mRatingBar.setStarFillDrawable(getResources().getDrawable(R.mipmap.star));
        mRatingBar.setStar(3f);

    }

    @Override
    public void doBusiness() {

        logout_ll.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
           showDialog();
          }
      });
        my_circle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                gotoActivity(MyCircleActivity.class,false);
            }
        });
        my_goods.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                gotoActivity(GoodsListActivity.class,false);
            }
        });
        my_share.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                gotoActivity(MyshareActivity.class,false);
            }
        });
        my_collect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                gotoActivity(MyCollectActivity.class,false);
            }
        });
        my_message.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                gotoActivity(TypeInCodeActivity.class,false);

            }
        });
        password_setting.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                gotoActivity(PasswordSettingActivity.class,false);
            }
        });
        profile_edit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
             gotoActivity(ProfileChangedActivity.class,false);
            }
        });

        check_version.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//
            }
        });

    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.e("msg", "url1:" + ShareUtil.getUserImg(this));

        if(ShareUtil.getUserImg(this)==null){
            return;
        }else if(ShareUtil.getUserImg(this).isEmpty()){
            Picasso.with(this).load(R.mipmap.user_icon).into(user_image);
        } else if(ShareUtil.getUserIndex(this)){
            Picasso.with(this).load(new File(ShareUtil.getUserImg(this))).into(user_image);
        }else{
            Picasso.with(this).load(GlobalConfig.ONLINE_HTTP+""+ShareUtil.getUserImg(this)).into(user_image);
        }
        account.setText(ShareUtil.getUserName(this));

        if (appDownloadManager != null) {
            appDownloadManager.resume();
        }
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_profile_center;
    }

    /**
     * 显示对话框
     */
    private void showDialog() {
        final LogoutDialog mdialog = new LogoutDialog(ProfileCenterActivity.this);
        //退出
        mdialog.setOnExitListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //如果对话框处于显示状态
                if (mdialog.isShowing()) {
                    mdialog.dismiss();//关闭对话框
                    gotoActivity(LoginActivity.class,true);
                    ShareUtil.saveUserData(ProfileCenterActivity.this,"","","","");
                    ShareUtil.saveUserIndex(ProfileCenterActivity.this,false);
                }
            }
        });
        //取消
        mdialog.setOnCancelListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mdialog != null && mdialog.isShowing()) {
                    mdialog.dismiss();
                }
            }
        });
        mdialog.show();
    }


    @Override
    public void showMessage(File file,String url) {

    }

    @Override
    public void navigateToHome(String name) {

    }

    @Override
    public void updateUI(String head_img, String address, int age, String area, String city, String id_card, String name, String province, String remarks, String sex, List<String> list) {

    }

    @Override
    public void onPause() {
        super.onPause();
        if (appDownloadManager != null) {
            appDownloadManager.onPause();
        }

    }


}
